package com.suncnpap.reader.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.ObjUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.reader.service.ReaderService;
import com.suncnpap.library.mapper.CollectionLocationMapper;
import com.suncnpap.library.model.Bookshelf;
import com.suncnpap.library.model.CollectionLocation;
import com.suncnpap.reader.mapper.ClassMapper;
import com.suncnpap.reader.mapper.GradeMapper;
import com.suncnpap.reader.mapper.ReaderMapper;
import com.suncnpap.reader.mapper.ReaderTypeMapper;
import com.suncnpap.reader.mapper.UnitMapper;
import com.suncnpap.reader.model.Class;
import com.suncnpap.reader.model.Grade;
import com.suncnpap.reader.model.Reader;
import com.suncnpap.reader.model.ReaderType;
import com.suncnpap.reader.model.Unit;

/** 
 * 说明： 读者Service实现类
 * @version
 */
@Service("readerService")
public class ReaderServiceImpl implements ReaderService{

	
	@Autowired
	ReaderMapper readerMapper;
	
	@Autowired
	GradeMapper gradeMapper;
	
	@Autowired
	ClassMapper classMapper;
	
	@Autowired
	ReaderTypeMapper readerTypeMapper;
	
	@Autowired
	CollectionLocationMapper collectionLocationMapper;
	
	@Autowired
	UnitMapper unitMapper;
	@Override
	public void add(Reader obj) {
		// TODO Auto-generated method stub
		readerMapper.add(obj);
	}

	@Override
	public void update(Reader obj) {
		// TODO Auto-generated method stub
		readerMapper.edit(obj);
	}

	@Override
	public void deleteById(String strId) {
		// TODO Auto-generated method stub
		readerMapper.deleteById(strId);
		
	}
	@Override
	public void deleteMore(String[] strIds) {
		// TODO Auto-generated method stub
		readerMapper.deleteMore(strIds);
	}

	@Override
	public Page list(Page page) {
		// TODO Auto-generated method stub
		List<Reader> list = readerMapper.datalistPage(page);
		page.setDataList(list);
		return page;
	}


	@Override
	public int findCount(Page page) {
		// TODO Auto-generated method stub
		return readerMapper.findCount(page);
	}

	@Override
	public Reader findById(String strId) {
		// TODO Auto-generated method stub
		return readerMapper.findById(strId);
	}

	@Override
	public void setLossById(String strId, Integer intIsReportLoss, String strSchoolId){
		// TODO Auto-generated method stub
		readerMapper.setLossById(strId, intIsReportLoss, strSchoolId);
		
	}
	
	@Override
	public void setStopById(String strId, Integer intIsStopBy, String strSchoolId){
		// TODO Auto-generated method stub
		readerMapper.setStopById(strId, intIsStopBy, strSchoolId);
		
	}
	
	@Override
	public Reader findByCardNumber(String strCardNumber, String strSchoolId){
		return readerMapper.findByCardNumber(strCardNumber, strSchoolId);
	}
	
	@Override
	public Reader findByStudentNumber(String strStudentNumber, String strSchoold){
		return readerMapper.findByStudentNumber(strStudentNumber, strSchoold);
	}
	
	@Override
	public Reader findByName(String strName, String strSchoold){
		return readerMapper.findByName(strName, strSchoold);
	}
	
	@Override
	public  void addList(List<Reader> objList){
		if(objList!=null && objList.size()>0){
			for(Reader reader:objList){
				if(reader!=null){
					Reader tempReader = readerMapper.findByCardNumber(reader.getStrCardNumber(), reader.getStrSchoolId());
					
					if(tempReader==null){
						String strSchoolId = reader.getStrSchoolId();
						String strGrade = reader.getStrGrade();
						String strClass = reader.getStrClass();
						Grade grade = gradeMapper.findByNameAndSchoolId(strGrade, strSchoolId);
						com.suncnpap.reader.model.Class tempClass = null;
						
						if(grade==null){
							grade = new Grade();
							BeanUtil.copyBean(reader, grade);
							grade.setStrName(strGrade);
							grade.setStrId(UuidUtil.get32UUID());
							gradeMapper.add(grade);
							
							tempClass = null;
						}else{
							tempClass = classMapper.findByNameAndGradeId(strClass, grade.getStrId(), strSchoolId);
						}
						
						if(tempClass==null){
							tempClass = new com.suncnpap.reader.model.Class();
							BeanUtil.copyBean(reader, tempClass);
							tempClass.setStrName(strClass);
							tempClass.setStrGradeId(grade.getStrId());
							tempClass.setStrId(UuidUtil.get32UUID());
							classMapper.add(tempClass);
						}
						reader.setIntIsReportLoss(0);
						reader.setIntIsStopBy(0);
						reader.setIntIntegral(0);
						reader.init();
						reader.setStrGradeId(grade.getStrId());
						reader.setStrClassId(tempClass.getStrId());
						
						String strReaderType = reader.getStrReaderType();
						ReaderType readerType = readerTypeMapper.findByName(strReaderType, strSchoolId);
						
						if(readerType!=null){
							reader.setStrReaderTypeId(readerType.getStrId());
						}
						
						String clName = reader.getStrCollectionLocation();
						CollectionLocation collectionLocation = collectionLocationMapper.findByName(clName, strSchoolId);
						if(collectionLocation==null){
							collectionLocation = new CollectionLocation();
							BeanUtil.copyBean(reader, collectionLocation);
							collectionLocation.setStrName(clName);
							collectionLocation.setStrId(UuidUtil.get32UUID());
							collectionLocationMapper.add(collectionLocation);
						}
						reader.setStrCollectionLocation(clName);
						reader.setStrCollectionLocationId(collectionLocation.getStrId());
						
						String strUnitName = reader.getStrUnitName();
						if(!strUnitName.equals("")){
							Unit unit = unitMapper.findByName(strUnitName, strSchoolId);
							if(unit!=null){
								reader.setStrUnitId(unit.getStrId());
							}else{
								unit = new Unit(); 
								BeanUtil.copyBean(reader, unit);
								unit.setStrNodeName(strUnitName);
								unit.setStrReaderType(reader.getStrReaderType());
								unit.setStrReaderTypeId(reader.getStrReaderTypeId()); 
								unit.setStrLibraryAddress(reader.getStrCollectionLocation());
								unit.setStrLibraryAddress(reader.getStrCollectionLocationId());
								unit.setStrId(UuidUtil.get32UUID());
								unitMapper.add(unit);
							}
						}
						
						tempReader = readerMapper.findByCardNumber(reader.getStrCardNumber(), reader.getStrSchoolId());
						if(tempReader==null){
							readerMapper.add(reader);
						}
					}
				}
			}
		}
		
	}
	
	@Override
	public Page integralList(Page page){
		List<Reader> list = readerMapper.integrallistPage(page);
		page.setDataList(list);
		return page;
	}
	
	@Override
	public int getObjectRanking(Page page){
		Integer intRanking = readerMapper.getObjectRanking(page);
		return intRanking;
	}
	
	@Override
	public void updatePassword(String[] strIds, String strPassword, String strSalt) throws Exception{
		// TODO Auto-generated method stub
		readerMapper.updatePassword(strIds, strPassword, strSalt);
	}

	@Override
	public Reader info(String card, String schoolId) {
		Reader reader=readerMapper.infoByid(card,schoolId);
		return reader;
	}

	@Override
	public Integer password(String strSchoolId, String userId, String enPassword) {
		Integer password = readerMapper.getPassword(strSchoolId, userId, enPassword);
		return password;
	}

}

